package Command.control;

import Command.commandInterfaces.Command;
import Command.commandInterfacesImpl.NoCommand;

import java.util.Arrays;
import java.util.concurrent.ForkJoinPool;

/**
 * 管理命令组
 * @Author: pengzx
 * @Description:
 * @Date: 17:03 2017/9/12
 * @Modified by: 1.0
 */
public class RemoteControl {
    //处理多个命令
    Command[] onCommands;
    Command[] offCommands;

    Command undoCommand;

    public RemoteControl()
    {
        onCommands = new Command[7];
        offCommands = new Command[7];
        NoCommand noCommand = new NoCommand();
        for (int i = 0; i < 7; i++) {
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
        undoCommand = noCommand;
    }

    /**
     * 设置命令行
     *
     * @param slot
     * @param onCommand
     * @param offCommand
     */
    public void setCommands(int slot, Command onCommand, Command offCommand)
    {
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

    public void OnButtonWasPressed(int slot)
    {
        onCommands[slot].execute();
        undoCommand = onCommands[slot];//记录命令所在的位置
    }

    public void OffButtonWasPressed(int slot)
    {
        offCommands[slot].execute();
        undoCommand = offCommands[slot];
    }

    public void  undoButtonWasPressed(){
        undoCommand.undo();
    }

    @Override
    public String toString()
    {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("---------RemoteControl---------\n");
        for (int i = 0; i < onCommands.length; i++) {
            stringBuffer.append("[slot" + i + "]" + onCommands[i].getClass().getName() + "     " + offCommands[i].getClass().getName() + "\n");
        }
        return stringBuffer.toString();
    }
}
